package com.neusoft.nep.SystemAdminstrator.Controller;

import com.neusoft.nep.GridOfficer.controller.GirdOfficerLoginViewController;
import com.neusoft.nep.GridOfficer.entity.Officer;
import com.neusoft.nep.SystemAdminstrator.Common.AlertUtills;
import com.neusoft.nep.SystemAdminstrator.Common.FileUtills;
import com.neusoft.nep.SystemAdminstrator.entity.SystemFeedback;
import javafx.collections.FXCollections;
import javafx.collections.ObservableArray;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class GridName {
    private int keyID;
    private String GridName;

    public int getKeyID() {
        return keyID;
    }

    public void setKeyID(int keyID) {
        this.keyID = keyID;
    }

    public String getGridName() {
        return GridName;
    }

    public void setGridName(String gridName) {
        GridName = gridName;
    }
}

public class PublicDataAssignController {
    private int i = 0;
    AlertUtills alertUtills = new AlertUtills();

    @FXML
    private ComboBox comName;
    @FXML
    private Label labID;
    Map<String, Officer> officerMap = FileUtills.readMapObject("GirdOfficer.txt");

    // 用于存储指派的网格员名字
    private List<String> assignedOfficerNames = new ArrayList<>();


    public void initialize() {
        labID.setText("系统管理员：" + loginController.currentLoginID);
        ObservableList<String> observableList = FXCollections.observableArrayList();
        for (String phonenumber : officerMap.keySet()) {
            observableList.add(officerMap.get(phonenumber).getName());
        }
        comName.setItems(observableList);

        // 从文件中读取已指派的网格员名字
        assignedOfficerNames = FileUtills.readListObject("AssignofficerNames.txt");
        if (assignedOfficerNames == null) {
            assignedOfficerNames = new ArrayList<>();
        }
    }

    @FXML
    private Button btn_out;

    public void OUT(ActionEvent actionEvent) {
        System.exit(0);
    }

    @FXML
    private ToggleButton tbtn_different;

    public void toDifferentAssign(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/PublicDataAssignDifferent.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        PublicDataAssignDifferentController publicDataAssignDifferentController = fxmlLoader.getController();
        publicDataAssignDifferentController.setSystemFeedback(systemFeedback);
        Scene scene = tbtn_different.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }

    @FXML
    private Button btn_assign;

    public void assign(ActionEvent event) throws IOException {
        String aaa = comName.getSelectionModel().getSelectedItem().toString();
        System.out.println(aaa);
        File newfile = new File(aaa + ".txt");

        try {
            newfile.createNewFile();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (newfile.exists()) {
            if (i != 0) {
                i = 0;
            }
            GirdOfficerLoginViewController.setOfficerName(aaa);
            alertUtills.Showdialog("指派", null, "指派成功");
            systemFeedback.setAssigned(true);
            Map<String, SystemFeedback> systemFeedbackMap = new HashMap<>();
            systemFeedbackMap.put(String.valueOf(systemFeedback.getIndex()), systemFeedback);
            FileUtills.writeMapObject(aaa + ".txt", systemFeedbackMap);
            Map<String, SystemFeedback> systemFeedbackMap1 = FileUtills.readMapObject("SystemFeedback.txt");
            systemFeedbackMap1.put(String.valueOf(systemFeedback.getIndex()), systemFeedback);
            FileUtills.writeMapObject("SystemFeedback.txt", systemFeedbackMap1);
            System.out.println(systemFeedback.getAqiLevel());

            // 将指派的网格员名字添加到集合中
            if (!assignedOfficerNames.contains(aaa)) {
                assignedOfficerNames.add(aaa);
                // 将集合保存到文件中
                FileUtills.writeListObject("AssignofficerNames.txt", assignedOfficerNames);
            }

            i++;
        } else {
            alertUtills.ShowErrorDialog("指派", null, "指派失败！");
        }
    }

    @FXML
    private Button btn_back;

    public void toback(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/publicList.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_back.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }

    SystemFeedback systemFeedback;

    private void initDataToUI() {
        if (systemFeedback != null) {
            infoIdValue.setText(String.valueOf(systemFeedback.getIndex()));
            feedbackPersonValue.setText(systemFeedback.getName() + " " + systemFeedback.getSex() + " " + systemFeedback.getBirthday());
            phoneValue.setText(systemFeedback.getPhoneNumber());
            addressValue.setText(systemFeedback.getAddress());
            descriptionValue.setText(systemFeedback.getDescription());
            levelValue.setText(systemFeedback.getAqiLevel());
            feedbackTimeValue.setText(systemFeedback.getTime());
        }
    }

    public void setSystemFeedback(SystemFeedback systemFeedback) {
        this.systemFeedback = systemFeedback;
        initDataToUI();
    }

    @FXML
    private Label infoIdValue;
    @FXML
    private Label feedbackPersonValue;
    @FXML
    private Label phoneValue;
    @FXML
    private Label addressValue;
    @FXML
    private Label descriptionValue;
    @FXML
    private Label levelValue;
    @FXML
    private Label feedbackTimeValue;
    @FXML
    private MenuItem mi_PSL;

    public void PSLClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/publicList.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }

    @FXML
    private MenuItem mi_AQI;

    public void AQIListClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/AQImakesure.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }

    @FXML
    private MenuItem mi_PGC;

    public void PGCClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/ProvincialGroupComponentItemInspectionStatistics.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }

    @FXML
    private MenuItem mi_AqiDistribute;

    public void AqiDistributeClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/AqiDistributeTotalStatisView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }

    @FXML
    private MenuItem mi_Other;

    public void OtherClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/OtherTotalStatisView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }

    @FXML
    private MenuItem mi_AqiTrendTotal;

    public void AqiTrendClick(ActionEvent event) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader();
        URL url = getClass().getResource("../View/AqiTrendTotalStatisView.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene = btn_out.getScene();
        scene.setRoot(root);
        Stage stage = (Stage) scene.getWindow();
        stage.show();
    }
}